//
//  AddPhotoViewController.m
//  YMPhotograph
//
//  Created by 玥明 许 on 16/7/1.
//  Copyright © 2016年 xym. All rights reserved.
//

#import "AddPhotoViewController.h"
#import "MyBlockViewController.h"
#import "Masonry.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "UIImage+GIF.h"
#define docPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
@interface AddPhotoViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@end

@implementation AddPhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getPhoto];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//从相册获取图片
-(IBAction)takePictureClick:(UIButton *)sender
{
    UIActionSheet* actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:@"请选择文件来源"
                                  delegate:self
                                  cancelButtonTitle:@"取消"
                                  destructiveButtonTitle:nil
                                  otherButtonTitles:@"照相机",@"本地相簿",nil];
    [actionSheet showInView:self.view];

}

#pragma mark -
#pragma UIActionSheet Delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"buttonIndex = [%ld]",buttonIndex);
    switch (buttonIndex) {
        case 0://照相机
        {
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

            imagePicker.delegate = self;
            imagePicker.allowsEditing = YES;
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:imagePicker animated:YES completion:nil];
        }
            break;
        case 1://本地相簿
        {
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
            imagePicker.delegate = self;
            imagePicker.allowsEditing = YES;
            imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            //            [self presentModalViewController:imagePicker animated:YES];
            [self presentViewController:imagePicker animated:YES completion:nil];
        }break;
        default:
            break;
    }
}

#pragma mark -
#pragma UIImagePickerController Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(__bridge NSString *)kUTTypeImage]) {
        UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
        [self performSelector:@selector(saveImage:)  withObject:img afterDelay:0.5];
    }
    //    [picker dismissModalViewControllerAnimated:YES];
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    //    [picker dismissModalViewControllerAnimated:YES];
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)saveImage:(UIImage *)image {
    //    NSLog(@"保存头像！");
    //    [userPhotoButton setImage:image forState:UIControlStateNormal];
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;

    NSString *documentsDirectory = docPath;
    NSString *imageFilePath = [documentsDirectory stringByAppendingPathComponent:@"selfPhoto"];
    NSLog(@"imageFile->>%@",imageFilePath);
    success = [fileManager fileExistsAtPath:imageFilePath];
    if(success) {
        success = [fileManager removeItemAtPath:imageFilePath error:&error];
    }
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, image.size.width, image.size.height)];
    imageView.image = image;
    [self.view addSubview:imageView];
    [UIImageJPEGRepresentation(image, 1.0f) writeToFile:imageFilePath atomically:YES];//写入文件
   // UIImage *selfPhoto = [UIImage imageWithContentsOfFile:imageFilePath];//读取图片文件
    AVFile *file = [AVFile fileWithName:@"1.jpg" contentsAtPath:imageFilePath];
    AVACL *acl = [AVACL ACL];
    [acl setReadAccess:YES forUser:[AVUser currentUser]];// 设置公开的「读」权限，任何人都可阅读
    [acl setWriteAccess:YES forUser:[AVUser currentUser]];// 为当前用户赋予「写」权
    file.ACL = acl;

    [file saveInBackground];

}
- (void)getPhoto
{
//    [AVFile getFileWithObjectId:@"57766a2f5bbb5000593938e1" withBlock:^(AVFile *file, NSError *error) {
//        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 150, 150)];
//        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:file.url]]];
//        imageView.image = image;
//        [self.vStackView addSubview:imageView];
//    }];
//    UIView *red = [[UIView alloc]init];
//    red.backgroundColor = [UIColor redColor];
//    [self.view addSubview:red];
//    [red mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.left.offset(10);
//        make.right.offset(-10);
//        make.height.mas_equalTo(150);
//    }];
//    UIView *blue = [[UIView alloc] init];
//    blue.backgroundColor = [UIColor blueColor];
//    [self.view addSubview:blue];
//    [blue mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(red.mas_bottom).offset(20);
//        make.right.offset(-20);
//        make.height.equalTo(red);
//        make.width.equalTo(red).multipliedBy(0.5);
//    }];
//
//    [MyBlockViewController ChangeRootViewBtnRect:[UIScreen mainScreen].bounds blockcompletion:^(UIColor *colorEnum) {
//        self.view.backgroundColor =colorEnum;
//    }];

    NSOperationQueue *opQuene = [[NSOperationQueue alloc] init];
    [opQuene addOperationWithBlock:^{
        NSString *str=  @"http://tu.lmstc.com/uploads/allimg/151103/10-151103094032438.gif";
        NSURL *url = [NSURL URLWithString:str];
        NSData *data = [NSData dataWithContentsOfURL:url];
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            UIImage *img= [[UIImage alloc]init];
            [img sd_animatedImageByScalingAndCroppingToSize:CGSizeMake(500, 500)];
            img = [UIImage sd_animatedGIFWithData:data];
            self.xlcImageview.image = img;
            [self.xlcImageview sizeToFit];
        }];
    }];

}



@end
